package io.renren.modules.driver.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.modules.driver.entity.SjDriverEntity;
import io.renren.modules.driver.service.SjDriverService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;




/**
 * 司机表
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-11-30 18:18:19
 */
@RestController
@RequestMapping("sjdriver")
public class SjDriverController {
	@Autowired
	private SjDriverService sjDriverService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sjdriver:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<SjDriverEntity> sjDriverList = sjDriverService.queryList(query);
		int total = sjDriverService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(sjDriverList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("sjdriver:info")
	public R info(@PathVariable("id") Long id){
		SjDriverEntity sjDriver = sjDriverService.queryObject(id);
		
		return R.ok().put("sjDriver", sjDriver);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("sjdriver:save")
	public R save(@RequestBody SjDriverEntity sjDriver){
		sjDriverService.save(sjDriver);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("sjdriver:update")
	public R update(@RequestBody SjDriverEntity sjDriver){
		if(!"".equals(sjDriver.getPassword()) && sjDriver.getPassword() != null) {
			String passWord = new Sha256Hash(sjDriver.getPassword()).toHex();
			sjDriver.setPassword(passWord);
		}
		sjDriverService.update(sjDriver);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("sjdriver:delete")
	public R delete(@RequestBody Long[] ids){
		sjDriverService.deleteBatch(ids);
		
		return R.ok();
	}




	/**
	 * 司机财务统计模块
	 */
	@RequestMapping("/sjFinancelist")
	@RequiresPermissions("sjdriver:sjFinancelist")
	public R sjFinancelist(@RequestParam Map<String, Object> params){
		Query query = new Query(params);
		List<SjDriverEntity> sjDriverList = sjDriverService.sjFinancelist(query);
		int total = sjDriverService.sjFinancelistTotal(query);

		PageUtils pageUtil = new PageUtils(sjDriverList, total, query.getLimit(), query.getPage());

		return R.ok().put("page", pageUtil);
	}
	
}
